public class LC2M {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag=0;
        ListNode ans=new ListNode();
        ListNode now=ans;
        while (l1!=null && l2!=null){
            now.val=l1.val+ l2.val+flag;
            if (now.val>=10){
                flag=1;
                now.val-=10;
            }
            else {
                flag=0;
            }
            l1=l1.next;
            l2=l2.next;
            if (l1!=null || l2!=null){
                now.next=new ListNode();
                now=now.next;
            }
        }
        while (true){
            if (l1==null){
                break;
            }
            now.val=l1.val+flag;
            if (now.val>=10){
                now.val-=10;
                flag=1;
            }
            else {
                flag=0;
            }
            l1=l1.next;
            if (l1!=null){
                now.next=new ListNode();
                now=now.next;
            }
        }
        while (true){
            if(l2==null){
                break;
            }
            now.val=l2.val+flag;
            if (now.val>=10){
                now.val-=10;
                flag=1;
            }
            else {
                flag=0;
            }
            l2=l2.next;
            if (l2!=null){
                now.next=new ListNode();
                now=now.next;
            }
        }
        if(flag==1){
            now.next=new ListNode(1);
        }
        return ans;
    }
}


